
var province,provinceId=-1,city,cityId=-1,area,areaId=-1;
var nameFlag = false;
var passwordFlag = false;
var genderFlag = false;
var addressFlag = false;
var agreeFlag = false;
var codeFlag = false;
var emailFlag = false;
$(document).ready(function(){
	
	$("#usernameText").blur(function(){
		if($("#usernameText").val() != ""){
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"checkUserName?user.username="+encodeURI(encodeURI($("#usernameText").val())),
		        data: {},
		        success:function(data,status) {
		        	if(data.isExist){
		        		$("#nameErrorMessage0").css("visibility","visible");
		        		$("#nameCorrectMessage0").css("visibility","hidden");
		        		nameFlag = false;
		        	}else{
		        		$("#nameCorrectMessage0").css("visibility","visible");
		        		$("#nameErrorMessage0").css("visibility","hidden");
		        		nameFlag = true;
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}else{
    		$("#nameCorrectMessage0").css("visibility","hidden");
    		$("#nameErrorMessage0").css("visibility","hidden");
    		nameFlag = false;
		}
	});
	
	$("#securityCodeImg").click(function(){
		 $(this).attr("src","SecurityCodeImageAction?timestamp="+new Date().getTime());
	});
	
	$("#securityCode").blur(function(){
		var secureCode = $(this).val();
		if(secureCode != ""){
			$.ajax({
		        type: "post",
		        url: $('#basePath').val()+"checkSecureCode?securityCode="+secureCode,
		        data: {},
		        success:function(data,status) {
		        	if(!data.isCorrect){
		        		$("#secutiryCodeError").css("visibility","visible");
		        		$("#secutiryCodeCorrect").css("visibility","hidden");
		        		codeFlag = false;
		        	}else{
		        		$("#secutiryCodeCorrect").css("visibility","visible");
		        		$("#secutiryCodeError").css("visibility","hidden");
		        		codeFlag = true;
		        	}
		        },
		        error:function(data,status){
		        	alert("ajax error.");
		        }
		     });
		}else{
			$("#secutiryCodeCorrect").css("visibility","hidden");
			$("#secutiryCodeError").css("visibility","hidden");
			codeFlag = false;
		}
	});
	
	$("#userEmailText").blur(function(){
		var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
		var email = $(this).val();
		if(email != ""){
			if(re.test(email)){
				$("#emailErrorMessage").css("visibility","hidden");
	    		$("#emailCorrectMessage").css("visibility","visible");
	    		emailFlag = true;
			}else{
				//email validate
				$.ajax({
			        type: "post",
			        url: $('#basePath').val()+"emailValidate?user.email="+email,
			        data: {},
			        success:function(data,status) {
			        	if(!data.isCorrect){
			        		$("#secutiryCodeError").css("visibility","visible");
			        		$("#secutiryCodeCorrect").css("visibility","hidden");
			        		codeFlag = false;
			        	}else{
			        		$("#secutiryCodeCorrect").css("visibility","visible");
			        		$("#secutiryCodeError").css("visibility","hidden");
			        		codeFlag = true;
			        	}
			        },
			        error:function(data,status){
			        	alert("ajax error.");
			        }
			     });
				
				
				$("#emailErrorMessage").css("visibility","visible");
	    		$("#emailCorrectMessage").css("visibility","hidden");
	    		emailFlag = false;
			}
		}else{
			emailFlag = false;
		}
	});
	
	$("#isAgree").click(function(){
		var cb = document.getElementById("isAgree");
		if(cb.checked){
			agreeFlag = true;
		}else{
			agreeFlag = false;
		}
	});
	
	
	var b = AreaSelector({
				selProvinceId:'selProvince',
				selCityId:'selCity',
				selAreaId:'selArea',
			    onProvinceChange:provinceChange,
			    onCityChange:cityChange,
			    onAreaChange:areaChange});
	
	b.initProvince(-1,-1,-1);
	
	function provinceChange(){
		provinceId = this.options[this.selectedIndex].value;
		province = _ds_data[this.options[this.selectedIndex].value].name;
		if(provinceId == 34){
			$("#userAddress").val(province);
		}
	}
	function cityChange(){
		cityId = this.selectedIndex-1;
		city=_ds_data[provinceId].city[this.selectedIndex-1].name;
		if(provinceId == 34){
			$("#userAddress").val(province);
		}
	}
	function areaChange(){
		area = _ds_data[provinceId].city[cityId].area[this.selectedIndex-1].name;
		if(province != undefined && city != undefined && area != undefined){
			$("#userAddress").val(province+city+area);
			alert($("#userAddress").val());
		}
	}
	
});
